package com.animerank.controller;

import com.animerank.entity.User;
import com.animerank.service.IUserService;
import com.animerank.dto.UserRegisterDTO;
import com.animerank.dto.UserLoginDTO;
import com.animerank.dto.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户注册、登录、信息查询等接口")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 用户注册接口
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户注册接口，需要提供用户名和密码")
    public Result<?> register(@Valid @RequestBody UserRegisterDTO userRegisterDTO) {
        return userService.register(userRegisterDTO);
    }

    /**
     * 用户登录接口
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录接口，成功后返回token")
    public Result<?> login(@Valid @RequestBody UserLoginDTO userLoginDTO, HttpServletResponse response) {
        return userService.login(userLoginDTO, response);
    }

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/current")
    @Operation(summary = "获取当前用户信息", description = "获取当前登录用户的基本信息")
    public Result<User> getCurrentUser() {
        return userService.getCurrentUser();
    }

    /**
     * 用户登出接口
     */
    @PostMapping("/logout")
    @Operation(summary = "用户登出", description = "用户登出接口")
    public Result<?> logout() {
        return userService.logout();
    }

}